package com.shiyi.controller.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.shiyi.service.ArticleService;
import com.shiyi.annotation.OperationLogger;
import com.shiyi.common.ResponseResult;
import com.shiyi.dto.ArticleDTO;
import com.shiyi.service.CloudOssService;
import com.shiyi.util.FilesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/system/article")
@RequiredArgsConstructor
@Api(tags = "后台文章管理")
public class ArticlesController {

    private final ArticleService articleService;
    //文件上传
    private final CloudOssService cloudOssService;

    @PostMapping(value = "/list")
    @SaCheckLogin
    @ApiOperation(value = "文章列表", httpMethod = "POST", response = ResponseResult.class, notes = "文章列表")
    public ResponseResult list(@RequestBody Map<String,Object> map) {
        return articleService.listArticle(map);
    }

    @GetMapping(value = "/info")
    @SaCheckPermission("/system/article/info")
    @ApiOperation(value = "文章详情", httpMethod = "GET", response = ResponseResult.class, notes = "文章详情")
    public ResponseResult getArticleById(Long id) {
        return articleService.getArticleById(id);
    }

    @PostMapping(value = "/add")
    @SaCheckPermission("/system/article/add")
    @ApiOperation(value = "保存文章", httpMethod = "POST", response = ResponseResult.class, notes = "保存文章")
    @OperationLogger(value = "保存文章")
    public ResponseResult insert(@RequestBody ArticleDTO article) {
        return  articleService.insertArticle(article);
    }

    @PostMapping(value = "/update")
    @SaCheckPermission("/system/article/update")
    @ApiOperation(value = "修改文章", httpMethod = "POST", response = ResponseResult.class, notes = "修改文章")
    @OperationLogger(value = "修改文章")
    public ResponseResult update(@RequestBody ArticleDTO article) {
        return articleService.updateArticle(article);
    }

    @DeleteMapping(value = "/delete")
    @SaCheckPermission("/system/article/delete")
    @ApiOperation(value = "删除文章", httpMethod = "DELETE", response = ResponseResult.class, notes = "删除文章")
    @OperationLogger(value = "删除文章")
    public ResponseResult delete(Long id) {
        return articleService.deleteArticle(id);
    }

    @DeleteMapping(value = "/deleteBatch")
    @SaCheckPermission("/system/article/deleteBatch")
    @ApiOperation(value = "批量删除文章", httpMethod = "DELETE", response = ResponseResult.class, notes = "批量删除文章")
    @OperationLogger(value = "批量删除文章")
    public ResponseResult deleteBatch(@RequestBody List<Long> ids) {
        return articleService.deleteBatchArticle(ids);
    }

    @PostMapping(value = "/top")
    /*@SaCheckPermission("/system/article/top")*/
    @ApiOperation(value = "置顶文章", httpMethod = "POST", response = ResponseResult.class, notes = "置顶文章")
    @OperationLogger(value = "置顶文章")
    public ResponseResult putTopArticle(@RequestBody ArticleDTO article) {
        return articleService.putTopArticle(article);
    }

    @PostMapping(value = "/pubOrShelf")
    @SaCheckPermission("/system/article/pubOrShelf")
    @ApiOperation(value = "发布或下架文章", httpMethod = "POST", response = ResponseResult.class, notes = "发布或下架文章")
    @OperationLogger(value = "发布或下架文章")
    public ResponseResult publishAndShelf(@RequestBody ArticleDTO article) {
        return articleService.publishAndShelf(article);
    }

    @PostMapping(value = "/baiduSeo")
    @SaCheckPermission("/system/article/baiduSeo")
    @ApiOperation(value = "文章SEO", httpMethod = "POST", response = ResponseResult.class, notes = "文章SEO")
    @OperationLogger(value = "文章SEO")
    public ResponseResult articleSeo(@RequestBody List<Long> ids) {
        return articleService.articleSeo(ids);
    }

    @GetMapping(value = "/reptile")
    @SaCheckPermission("/system/article/reptile")
    @ApiOperation(value = "文章爬虫", httpMethod = "GET", response = ResponseResult.class, notes = "文章爬虫")
    @OperationLogger(value = "文章爬虫")
    public ResponseResult reptile(String url) {
        return articleService.reptile(url);
    }

    @GetMapping(value = "/randomImg")
    @ApiOperation(value = "随机获取一张图片", httpMethod = "GET", response = ResponseResult.class, notes = "随机获取一张图片")
    public ResponseResult randomImg() {
        return articleService.randomImg();
    }


    /**
     * （微秘书）获取每日新闻
     * @Scheduled 表示定时，每天早上6点执行
     */
    @GetMapping(value = "/getDaysNews")
    @Scheduled(cron ="0 25 5 * * ?")
    public ResponseResult getDaysNews() {
        return articleService.getDaysNews();
    }

    /**
     * （微秘书）获取历史上的今天 发送到群里
     * @Scheduled 表示定时，每天早上8点30执行
     */
    @GetMapping(value = "/getTodayInHistory")
    @Scheduled(cron ="0 30 8 * * ?")
    public ResponseResult getTodayInHistory() {
        return articleService.getTodayInHistory();
    }


    /**
     * （微秘书）获取IT资讯的新闻，发送到群里
     * @Scheduled 表示定时，每天早上8点30执行
     * @param
     */
    @GetMapping(value = "/getDaysITNews")
    @Scheduled(cron ="0 30 5 * * ?")
    public ResponseResult getDaysITNews() {
        return articleService.getDaysITNews();
    }


    /**
     * （测试用，现在不用了）将每日新闻让推送到微信群
     */
    @GetMapping(value = "/sendWechat")
    public void sendWechat(){
       // articleService.sendWechat();
    }

    /**
     * 早安寄语
     */
    @GetMapping(value = "/goodMorning")
    public ResponseResult goodMorning(String tx_key){
      // return ResponseResult.success(articleService.goodMorning(tx_key));
        return ResponseResult.success("早安寄语！！");
    }

    /**
     * （微秘书）发送消息给指定的人
     */
    @GetMapping(value = "/sendWechatToUser")
    @Scheduled(cron ="0 10 7 * * ?")
    public ResponseResult sendWechatToUser(){
        articleService.sendWechatToUser();
        return ResponseResult.success();
    }

    /**
     * 手动上传网络图片
     */
    @GetMapping(value = "/uploadFm")
    public ResponseResult uploadFm(){
        MultipartFile multipartFile =  FilesUtil.onlineAddressTransferFile("https://img-community.csdnimg.cn/images/86f32eac875e42af9ed9e91b809dc7d8.png");
        return cloudOssService.upload(multipartFile,null);
    }

    /**
     * @Description 獲取每日新聞生成图片
     * @Author 穆雄雄
     * @Date:2023/3/13 下午 10:49
     * 博客：https://blog.csdn.net/qq_34137397
     * 个人站：https://www.穆雄雄.com
     * 个人站：https://www.muxiongxiong.cn
     * 个人站：https://88688.team
     * @Param []
     * @return
     **/
    @GetMapping(value = "/getDaysNewsConvertImage")
    public ResponseResult getDaysNewsConvertImage(){
        return ResponseResult.success( articleService.getDaysNewsConvertImage());
    }


    /**
     * @Description 获取今天的日期
     * @Author 穆雄雄
     * @Date:2023/3/15 下午 10:41
     * 博客：https://blog.csdn.net/qq_34137397
     * 个人站：https://www.穆雄雄.com
     * 个人站：https://www.muxiongxiong.cn
     * 个人站：https://88688.team
     * @Param []
     * @return com.shiyi.common.ResponseResult
     **/
    @GetMapping(value = "/getTodaysInfo")
    public ResponseResult getTodaysInfo(){
        return ResponseResult.success(articleService.getTodaysInfo());
    }


}
